package com.xiaoyu.lanling.feature.vip.activity

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.vip.VipProductItemClickEvent
import com.xiaoyu.lanling.event.vip.VipProductListEvent
import com.xiaoyu.lanling.feature.vip.data.VipData
import com.xiaoyu.lanling.feature.vip.fragment.VipProductFragment
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.FragmentAdapter
import com.xiaoyu.lanling.widget.tablayout.ScaleTabLayoutUtils
import kotlinx.android.synthetic.main.vip_product_activity.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VipProductListActivity : AppCompatToolbarActivity() {

    private val requestTag = Any()
    private val vipData = VipData()

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.vip_product_activity)
        initToolbar()

        initView()
        initData()
        initEvent()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_vip_product_list, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.mine -> Router.instance.gotoVipMineActivity(this)
        }
        return super.onOptionsItemSelected(item)
    }

    private fun initView() {
        title = getString(R.string.vip_product_list_title)
    }

    private fun initData() {
        vipData.getVipProductList(requestTag)
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: VipProductListEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                showData(event)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: VipProductItemClickEvent) {
                val from = intent.getStringExtra(Constant.PARAM_FROM)
                Router.instance.gotoVipBuyActivity(this@VipProductListActivity, event.productItem, from)
            }
        })
    }

    private fun showData(event: VipProductListEvent) {
        val fragments = ListUtil.convertToList(event.vipProducts) { vipProduct -> VipProductFragment.newInstance(vipProduct) }
        val titles: MutableList<CharSequence> = ListUtil.convertToList(event.vipProducts) { vipProduct -> vipProduct.vipInfo.name }

        ScaleTabLayoutUtils.createAccentScaleTabLayout(tab_layout, view_pager, titles, true)

        val fragmentAdapter = FragmentAdapter(supportFragmentManager, fragments, titles)
        view_pager.offscreenPageLimit = fragments.size
        view_pager.adapter = fragmentAdapter
        view_pager.currentItem = event.pageIndex
    }
}